package com.zkw.group.controller;

import com.alibaba.fastjson.JSON;
import com.zkw.group.entity.CourseRelation;
import com.zkw.group.entity.QueryInfo;
import com.zkw.group.service.CourseRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(description = "管理课程有关先修后修信息")
@CrossOrigin
@RestController
@RequestMapping("/CourseRelations")
public class CourseRelationController {

    @Autowired
    private CourseRelationService courseRelationService;

    @ApiOperation(value = "分页查询课程先修后修关系列表")
    @GetMapping("/AllCourseRelation")
    public String getAllCourseRelationList(QueryInfo queryInfo) {
        int numbers = courseRelationService.getCourseRelationCounts(queryInfo.getQuery(),queryInfo.getRole());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<CourseRelation> courseRelations = courseRelationService.getAllCourseRelationList(queryInfo.getQuery(),queryInfo.getRole(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",courseRelations);
        return JSON.toJSONString(res);
    }

    @ApiOperation(value = "添加课程先修后修关系")
    @PostMapping("/CourseRelation")
    public String addCourseRelation(@RequestBody CourseRelation courseRelation) {
        int result =  courseRelationService.addCourseRelation(courseRelation);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

    @ApiOperation(value = "删除指定先修后修关系")
    @DeleteMapping("/CourseRelation")
    public String deleteCourseRelation(String courseid,String relacname) {
        int result = courseRelationService.deleteCourseRelation(courseid,relacname);
        if (result >= 1) {
            return "success";
        } else {
            return "error";
        }
    }

}
